<template>
  <div
    ref="loadingOverlay"
    v-show="loading"
    class="loading-overlay"
    :style="{ width: parentWidth + 'px', height: parentHeight + 'px' }"
  >
    <div class="loading-content">
      <div class="loading-svg">
        <svg-icon :icon-class="svgClass" class="loading-content-svg-rotate" size="1.5"/>
      </div>
      <div class="loading-text">{{ loadingText }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Loading",
  props: {
    loading: {
      type: Boolean,
      default: () => false,
    },
    svgClass: {
      type: String,
      default: () => "loading",
    },
    loadingText: {
      type: String,
      default: () => "加载中......",
    },
  },
  data() {
    return {
      parentWidth: 0,
      parentHeight: 0,
    };
  },
  created() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.position();
      }, 500);
    });
  },
  methods: {
    position() {
      const parentElement = this.$refs.loadingOverlay.parentElement;
      this.parentWidth = parentElement.clientWidth;
      this.parentHeight = parentElement.clientHeight;
    },
  },
};
</script>

<style lang="scss" scoped>
.loading-overlay {
  position: fixed;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
}

.loading-content {
  /* 样式按需定制 */
  color: #409eff;
  font-weight: bold;
  margin: 40% 40%;

  div {
    float: left;
  }

  .loading-svg {
    margin-right: 15px;
  }

  .loading-text {
    font-size: 20px;
  }
}

.loading-content-svg-rotate {
  animation: loadingContentSvgRotate 2s linear infinite;
}

@keyframes loadingContentSvgRotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
